package com.jyw.custom.huaxi.oa.domain.dto;

import com.jyw.common.core.domain.entity.SysDept;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.StringUtils;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysDeptService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import javax.xml.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@XmlRootElement(name = "Request")
@XmlAccessorType(XmlAccessType.PROPERTY)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class HuaxiDeptPushDto {


    private Header header;

    private Body body;

    @XmlElement(name = "Header")
    public Header getHeader() {
        return header;
    }

    @XmlElement(name = "Body")
    public Body getBody() {
        return body;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @XmlRootElement(name = "Header")
    @XmlAccessorType(XmlAccessType.PROPERTY)
    @Data
    public static class Header {


        private String sourceSystem;

        private String messageID;

        @XmlElement(name = "SourceSystem")
        public String getSourceSystem() {
            return sourceSystem;
        }

        @XmlElement(name = "MessageID")
        public String getMessageID() {
            return messageID;
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @XmlRootElement(name = "Body")
    @XmlAccessorType(XmlAccessType.PROPERTY)
    @Data
    public static class Body {


        private List<CTDept> ctDeptList;

        @XmlElement(name = "CT_Dept")
        @XmlElementWrapper(name = "CT_DeptList")
        public List<CTDept> getCtDeptList() {
            return ctDeptList;
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @XmlRootElement(name = "CT_Dept")
    @XmlAccessorType(XmlAccessType.PROPERTY)
    @Data
    @Slf4j
    public static class CTDept {


        private String businessFieldCode;


        private String ctdCode;


        private String ctdDesc;


        private String ctdCategoryCode;


        private String ctdPhonetic;


        private String ctdWardFlag;


        private String ctdCreatDate;


        private String ctdCreatTime;


        private String ctdUpdateDate;


        private String ctdUpdateTime;


        private String ctdOriginalCode;


        private String ctdOriginalDesc;


        private String ctdRemarks;


        private String ctdStartDate;


        private String ctdEndDate;


        private String ctdCodesystemCode;


        private String ctdOfficeAddress;


        private String ctdOfficePhone;


        private String ctdUpdateUserCode;


        private String ctdHosCode;


        private String ctdDepartmentGroup;


        private String ctdAdmType;


        private String ctdParentCode;


        private String ctdStatus;


        private String ctdParentDeptCode;


        private String ctdFirstLevel;


        private String ctdSecondLevel;


        private String ctdThirdLevel;


        private String ctdFourthLevel;


        private String ctdFifthLevel;


        private String ctdVirtualFlag;


        private String ctdWSGType;

        @XmlElement(name = "BusinessFieldCode")
        public String getBusinessFieldCode() {
            return businessFieldCode;
        }

        @XmlElement(name = "CTD_Code")
        public String getCtdCode() {
            return ctdCode;
        }

        @XmlElement(name = "CTD_Desc")
        public String getCtdDesc() {
            return ctdDesc;
        }

        @XmlElement(name = "CTD_CategoryCode")
        public String getCtdCategoryCode() {
            return ctdCategoryCode;
        }

        @XmlElement(name = "CTD_Phonetic")
        public String getCtdPhonetic() {
            return ctdPhonetic;
        }

        @XmlElement(name = "CTD_WardFlag")
        public String getCtdWardFlag() {
            return ctdWardFlag;
        }

        @XmlElement(name = "CTD_CreatDate")
        public String getCtdCreatDate() {
            return ctdCreatDate;
        }

        @XmlElement(name = "CTD_CreatTime")
        public String getCtdCreatTime() {
            return ctdCreatTime;
        }

        @XmlElement(name = "CTD_UpdateDate")
        public String getCtdUpdateDate() {
            return ctdUpdateDate;
        }

        @XmlElement(name = "CTD_UpdateTime")
        public String getCtdUpdateTime() {
            return ctdUpdateTime;
        }

        @XmlElement(name = "CTD_OriginalCode")
        public String getCtdOriginalCode() {
            return ctdOriginalCode;
        }

        @XmlElement(name = "CTD_OriginalDesc")
        public String getCtdOriginalDesc() {
            return ctdOriginalDesc;
        }

        @XmlElement(name = "CTD_Remarks")
        public String getCtdRemarks() {
            return ctdRemarks;
        }

        @XmlElement(name = "CTD_StartDate")
        public String getCtdStartDate() {
            return ctdStartDate;
        }

        @XmlElement(name = "CTD_EndDate")
        public String getCtdEndDate() {
            return ctdEndDate;
        }

        @XmlElement(name = "CTD_CodesystemCode")
        public String getCtdCodesystemCode() {
            return ctdCodesystemCode;
        }

        @XmlElement(name = "CTD_OfficeAddress")
        public String getCtdOfficeAddress() {
            return ctdOfficeAddress;
        }

        @XmlElement(name = "CTD_OfficePhone")
        public String getCtdOfficePhone() {
            return ctdOfficePhone;
        }

        @XmlElement(name = "CTD_UpdateUserCode")
        public String getCtdUpdateUserCode() {
            return ctdUpdateUserCode;
        }

        @XmlElement(name = "CTD_HosCode")
        public String getCtdHosCode() {
            return ctdHosCode;
        }

        @XmlElement(name = "CTD_DepartmentGroup")
        public String getCtdDepartmentGroup() {
            return ctdDepartmentGroup;
        }

        @XmlElement(name = "CTD_AdmType")
        public String getCtdAdmType() {
            return ctdAdmType;
        }

        @XmlElement(name = "CTD_ParentCode")
        public String getCtdParentCode() {
            return ctdParentCode;
        }

        @XmlElement(name = "CTD_Status")
        public String getCtdStatus() {
            return ctdStatus;
        }

        @XmlElement(name = "CTD_ParentDeptCode")
        public String getCtdParentDeptCode() {
            return ctdParentDeptCode;
        }

        @XmlElement(name = "CTD_FirstLevel")
        public String getCtdFirstLevel() {
            return ctdFirstLevel;
        }

        @XmlElement(name = "CTD_SecondLevel")
        public String getCtdSecondLevel() {
            return ctdSecondLevel;
        }

        @XmlElement(name = "CTD_ThirdLevel")
        public String getCtdThirdLevel() {
            return ctdThirdLevel;
        }

        @XmlElement(name = "CTD_FourthLevel")
        public String getCtdFourthLevel() {
            return ctdFourthLevel;
        }

        @XmlElement(name = "CTD_FifthLevel")
        public String getCtdFifthLevel() {
            return ctdFifthLevel;
        }

        @XmlElement(name = "CTD_VirtualFlag")
        public String getCtdVirtualFlag() {
            return ctdVirtualFlag;
        }

        @XmlElement(name = "CTD_WSGType")
        public String getCtdWSGType() {
            return ctdWSGType;
        }

        public SysDept buildDept(){
            ISysDeptService deptService = SpringUtils.getBean(ISysDeptService.class);

                //根据科室编号获取科室
                SysDept deptDataBase = deptService.lambdaQuery()
                        .eq(SysDept::getDeptCode, getCtdCode()).one();

                SysDept dept = new SysDept();
                if(!ObjectUtils.isEmpty(deptDataBase)){
                    dept.setDeptId(deptDataBase.getDeptId());
                }

                //获取上级科室
                SysDept parentDept = deptService.lambdaQuery().eq(SysDept::getDeptCode,getCtdParentCode()).one();
                if(!ObjectUtils.isEmpty(parentDept)){
                    dept.setParentId(parentDept.getDeptId());
                    dept.setAncestors(parentDept.getAncestors()+","+parentDept.getDeptId());
                }else {
                    if(StringUtils.isEmpty(getCtdParentCode())){
                        dept.setParentId(0l);
                        dept.setAncestors("0");
                    }
                }
                dept.setDeptCode(getCtdCode());
                dept.setDeptName(getCtdDesc());
                dept.setDeptPlace(getCtdOfficeAddress());
                dept.setPhone(getCtdOfficePhone());
                switch (getCtdStatus()){
                    case "-1":
                        dept.setStatus(deptDataBase.getStatus());
                        dept.setDelFlag("2");
                        break;
                    case "0":
                        dept.setStatus("1");
                        dept.setDelFlag("0");
                        break;
                    case "1":
                        dept.setStatus("0");
                        dept.setDelFlag("0");
                        break;
                }
                dept.setDeptRemark(getCtdRemarks());
                dept.setDeptType(0);
                if(!ObjectUtils.isEmpty(dept.getDeptId())){
                    dept.setCreateBy("admin");
                    dept.setCreateTime(DateUtils.getNowDate());
                }else{
                    dept.setUpdateBy("admin");
                    dept.setUpdateTime(DateUtils.getNowDate());
                }
                return dept;
        }
    }
}